/*
 * Jenny Ruppen 09-730-391
 * Sebastien Goldberg 08-914-855
 * Henry Raymond 08-928-061
 * Stefan Mussato 09-752-270
 */
 
package ch.uzh.ifi.sopra.gruppe9.game;

import com.jme3.scene.Node;


/**
 * Draws a waiting wheel in the GUI.
 */
public class GuiWaiting extends Node implements IUpdatable {
	
	
	/**
	 * Offset of the coordinate system of this waiting wheel.
	 */
	private GUI.Offset offset;
	
	
	/**
	 * Position of the waiting wheel in the x axis. (Assuming the window has a width of 1024 pixel.)
	 */
	private int positionX;
	
	
	/**
	 * Position of the waiting wheel in the y axis. (Assuming the window has a height of 768 pixel.)
	 */
	private int positionY;
	
	
	/**
	 * Width and height of the waiting wheel. (Assuming the window has dimensions of 1024 x 768 pixel.)
	 */
	private int size;
	
	
	/**
	 * Z-Index of the element. An element with a higher index will cover an element with a lower one.
	 */
	private int zIndex;
	
	
	/**
	 * Timer for animation. 
	 */
	private float timer = 0.0f;
	
	
	/**
	 * Geometry of spokes of waiting wheel.
	 */
	private GuiQuad[] spokes = new GuiQuad[8];
	
	
	/**
	 * Constructor
	 */
	public GuiWaiting(GUI.Offset offset, int positionX, int positionY, int size, int zIndex) {
		this.offset = offset;
		this.positionX = positionX;
		this.positionY = positionY;
		this.size = size;
		this.zIndex = zIndex;
		
		for(int i = 0; i < 8; i++) {
			spokes[i] = new GuiQuad();
			spokes[i].setAbsolutePositionSize(-30, 50, 60, 100, 0);
			spokes[i].setMaterial(MaterialManager.getWaiting(), 0.0f, 0.0f, 0.06666f, 1.0f);
			spokes[i].rotate(0.0f, 0.0f, (float)i * 45.0f * Scenery.DEG2RAD);
			this.attachChild(spokes[i].create());
		}
		
		this.setLocalScale((float)this.size / 300.0f, (float)this.size / 300.0f, 1.0f);
		this.setLocalTranslation(GUI.calculateX(this.offset, this.positionX + (this.size / 2)), GUI.calculateY(this.offset, this.positionY + (this.size / 2)), (float)this.zIndex);
		
	}


	@Override
	public void update(float delta) {
		timer += delta;
		while(timer > 1.0f) {
			timer -= 1.0f;
		}
		int animationStep = (int)Math.floor(timer * 32.0f);
		
		for(int i = 0; i < 8; i++) {
			animationStep = (animationStep + 4) % 32;
			if(animationStep < 15) {
				spokes[i].changeTextureCoords((float)animationStep * 0.06666f, 0.0f, 0.06666f, 1.0f);
			} else {
				spokes[i].changeTextureCoords(0.0f, 0.0f, 0.06666f, 1.0f);
			}
		}
		
	}
	

}
